package com.src.xyzk_personal.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;

import com.src.xyzk_personal.MainActivity;
import com.src.xyzk_personal.SettingActivity;
import com.src.xyzk_personal.config.Common;

import org.apache.tools.ant.Main;

import java.util.Locale;

/**
 * created by sjx on 2024/04/15
 * 切换语言工具类
 */
public class LanguageUtil {

    public static void setLocale(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }

    public static void changeLanguage(Context context, String language) {
        SharedPreferencesUtil.saveString(context, "language", language);
        setLocale(context, language);

        // restart MainActivity
        if (context instanceof Activity) {
            restartMainActivity((Activity) context);
        }

    }

    /**
     * 切换语言后重新加载MainActivity，主要为了加载配置文件
     * @param currentActivity
     */
    public static void restartMainActivity(Activity currentActivity) {
        Intent intent = new Intent(currentActivity, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        currentActivity.startActivity(intent);
        currentActivity.finish();
    }

    public static String changeConfigFile() {
        String configFile = null;
        switch (Common.Lang) {
            case "en": {
                configFile = Common.Dir + Common.GuestName + "/config-en.ini";
                break;
            }
            case "ru": {
                configFile = Common.Dir + Common.GuestName + "/config-ru.ini";
                break;
            }
            case "zh": {
                configFile = Common.Dir + Common.GuestName + "/config.ini";
                break;
            }
        }
        return configFile;
    }

}



